/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-7-11 下午2:31 Created by sky
 */
package com.jade.modules.message.sms.comm;

import com.jade.framework.base.JadeSystem;
import com.jade.modules.message.common.StatusMapper;

/**
 * 抽象的协议适配器。
 *
 * @author sky
 * @version 1.0 13-7-11 下午2:31
 */
public abstract class AbstractAdapter
        implements ProtocolAdapter
{
    protected SmsConfiguration configuration;
    protected StatusMapper statusMapper;

    public void setConfiguration(SmsConfiguration configuration)
    {
        this.configuration = configuration;
    }

    protected void loadStatusMapper()
    {
        if (statusMapper == null) {
            synchronized (this) {
                if (statusMapper == null) {
                    statusMapper = new StatusMapper();
                    try {
                        statusMapper.load(configuration.getStatusMappperResource());
                    }
                    catch (Throwable t) {
                        JadeSystem.getLog().warn("Can't load status mapper:" + configuration.getStatusMappperResource(),
                                t);
                    }
                }
            }
        }
    }

    protected int getProtocolStatus(int status)
    {
        loadStatusMapper();
        return statusMapper.getProtocolStatus(status);
    }

    protected int getStatus(int protocolStatus)
    {
        loadStatusMapper();
        return statusMapper.getStatus(protocolStatus);
    }
}
